const app = getApp()
var base64 = require("../../images/base64");
// 录音对象
const recorderManager = wx.getRecorderManager();

function sendRecord(src) {
  var obj = {
    url: "",
    filePath: src,
    name: "fffile",
    header: {
      'Content-Type': 'application/json'
    },
    success: function (result) {
      var data = JSON.parse(result.data);
      // msg 为最终语音识别的字符串
      var msg = data.result;
      // 获取当前页面对象
      var page = getCurrentPages()[0];
      page.setData({ msg: msg });
    },
    fail: function (err) {
      console.log(err);
    }
  };
  wx.uploadFile(obj)
}

// 结束录音的时候触发 
recorderManager.onStop((res) => {
  // 获取文件路径-提交到后台-后台发送到百度
  sendRecord(res.tempFilePath);
})

recorderManager.onError((res) => {
  console.log("error", res);
});

Page({
  data: {

    /**      * 页面配置      */
    winWidth: 0, winHeight: 0,
    // tab切换   
    height: 0,
    currentTab: 0,
    flag: false,
    inputShowed: false,
    inputVal: "",
    banners: [],
    tttype: [],
    host:app.d.hostImg,
    hotGoods:[],
    distance1: [],
    distance2: [],
    distance3: [],
    page1: 2,
    page2: 2,
    page3: 2,
    act_modal_list:'show',
    yjqflag:0,
    requestOrderList: [], // 获取订单的数据
    requestPageNum: 1,   // 设置加载的第几次，默认是第一次  
    callbackcount: 10,      //返回数据的个数 ，前端自己可以定制返回数据的个数
    requestLoading: true, //"上拉加载"的变量，默认false，隐藏  
    requestLoadingComplete: false  //“没有数据”的变量，默认false，隐藏
  },
  showInput: function () {

    wx.navigateTo({
      url: '../searchPage/searchPage',
    })
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  onLoad: function (options) {

    var that = this;
    setTimeout(function () {
      wx.request({
        url: app.d.ceshiUrl + '/Api/BIndex/show_getmore',
        method: 'post',
        data: {
          user_id: app.d.userId,
          type: 'all',
          page: 1,
          fid:0
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res)
          // if (res.data.store_id) {
          //   wx.redirectTo({
          //     url: '../index/index?store_Id=' + res.data.store_id,
          //   })
          // } else {
            var ad = res.data.ad;
            var ttype = res.data.type;
            var seller = res.data.place_store;
            var hotgood = res.data.bpro;
            var yjqflag = res.data.yjqflag;
            that.setData({
              banners: ad,
              tttype: ttype,
              sseller: seller,
              hotGoods: hotgood,
              yjqflag:yjqflag
            });
            that.setData({
              height: 176 * that.data.sseller.length,
            })
            wx.getLocation({
              type: 'gcj02',
              success: function (res) {
                var long2 = res.longitude
                var la2 = res.latitude
                that.setData({
                  long2: long2,
                  la2: la2
                })
                app.globalData.latitude = res.latitude
                app.globalData.longitude = res.longitude
                console.log(app.globalData.latitude, app.globalData.longitude)

                for (var i = 0; i < that.data.sseller.length; i++) {
                  var la1 = parseFloat(that.data.sseller[i].latitude)
                  var long1 = parseFloat(that.data.sseller[i].longitude)
                  var rad1 = la1 * Math.PI / 180.0;
                  var rad2 = la2 * Math.PI / 180.0;
                  var a = rad1 - rad2;
                  var b = long1 * Math.PI / 180.0 - long2 * Math.PI / 180.0;
                  var r = 6378.137;
                  var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
                  that.setData({
                    distance1: that.data.distance1.concat({ distance: distance.toFixed(1) })
                  })
                }
              },
              fail: function (e) {
                wx.showToast({
                  title: '网络不给力！',
                  duration: 2000
                });
              },
            })
          // }
        },
        fail: function (e) {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        },
      })
      wx.request({
        url: app.d.ceshiUrl + '/Api/News/newList',
        method: 'post',
        data: {
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          that.setData({
            list: res.data.list
          })
        },
        fail: function (e) {
          wx.showToast({
            title: '网络不给力！',
            duration: 2000
          });
        },
      })
    }, 1000);
    this.setData({
      icon: base64.icon20,
    })
    /**     * 获取系统信息     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowWidth*0.8
        });
      }
    })
  },
getShopList:function(types,page){
  var that=this
  var page=page;
  var types=types;
//onLoad   this.getShopList(types,page)
//点击 滑动  this.getShopList(types,page)
//对currentTab进行判断
  wx.request({
    url: app.d.ceshiUrl + '/Api/BIndex/show_getmore',
    method: 'post',
    data: {
      page:page,
      type:types,
      long2: typeof(that.data.long2)=='undefined'?0:that.data.long2,
      la2: typeof(that.data.la2)=='undefined'?0:that.data.la2,
    },
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      that.setData({
          requestLoadingComplete: false,
          requestLoading: true
      })
      //距离最近
      if (that.data.currentTab==0){
        var seller = res.data.place_store;
        var distance1 = []
        that.setData({
          sseller: seller,
          distance1: distance1,
          height: 176 * that.data.sseller.length,
          page1: 2
        });
        //销量最高
      } else if (that.data.currentTab == 1) {
        var store1 = res.data.num_store;
        var distance2 = []
        that.setData({
          store1: store1,
          distance2: distance2
        });
        that.setData({
          height1: 176 * that.data.store1.length,
        })
        for (var i = 0; i < that.data.store1.length; i++) {
          var la1 = parseFloat(that.data.store1[i].latitude)
          var long1 = parseFloat(that.data.store1[i].longitude)
          var rad1 = la1 * Math.PI / 180.0;
          var rad2 = that.data.la2 * Math.PI / 180.0;
          var a = rad1 - rad2;
          var b = long1 * Math.PI / 180.0 - that.data.long2 * Math.PI / 180.0;
          var r = 6378.137;
          var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
          that.setData({
            distance2: that.data.distance2.concat({ distance: distance.toFixed(1) })
          })
        }
        that.setData({
          page2: 2
        })
        //评价最高
      } else if (that.data.currentTab == 2) {
        var store2 = res.data.grade_store;
        var distance3 = []
        that.setData({
          store2: store2,
          distance3: distance3
        });
        that.setData({
          height2: 176 * that.data.store2.length,
        })
        for (var i = 0; i < that.data.store2.length; i++) {
          var la1 = parseFloat(that.data.store2[i].latitude)
          var long1 = parseFloat(that.data.store2[i].longitude)
          var rad1 = la1 * Math.PI / 180.0;
          var rad2 = that.data.la2 * Math.PI / 180.0;
          var a = rad1 - rad2;
          var b = long1 * Math.PI / 180.0 - that.data.long2 * Math.PI / 180.0;
          var r = 6378.137;
          var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
          that.setData({
            distance3: that.data.distance3.concat({ distance: distance.toFixed(1) })
          })
        }
        that.setData({
          page3: 2
        })
      }
    },
    fail: function (e) {
      wx.showToast({
        title: '网络不给力！',
        duration: 2000
      });
    },
  })
},

  toLive: function () {
    wx.navigateTo({
      url: '../live/live',
    })
  },
  toYhq: function () {
    wx.navigateTo({
      url: '../yhq/yhq',
    })
  },
  toJrkb:function(){
    wx.navigateTo({
      url: '../jrkb/jrkb',
    })
  },
  toNewsDetail:function(e){
    var news_id=e.target.id
    wx.navigateTo({
      url: '../newsDetail/newsDetail?news_id=' + news_id,
    })
  },

onShow:function(){
  app.globalData.froms = 'company_user'
  console.log(app.globalData.froms)
},
  /**     * 滑动切换tab     */
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    if (that.data.currentTab==0){
      this.getShopList('all', 1)
    } else if (that.data.currentTab == 1) {
      this.getShopList('num', 1)
    } else if (that.data.currentTab == 2) {
      this.getShopList('grade', 1)
    }

  },
  /**    * 点击tab切换    */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) { return false; }
    else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (this.data.currentTab == 0) {
      this.getShopList('all', 1)
    } else if (this.data.currentTab == 1) {
      this.getShopList('num', 1)
    } else if (this.data.currentTab == 2) {
      this.getShopList('grade', 1)
    }
  },
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  // tapBanner: function (e) {
  //   if (e.currentTarget.dataset.id != 0) {
  //     wx.navigateTo({
  //       url: "../" + (e.currentTarget.dataset.id == 1001 ? '' : '')
  //        预留商品详情
  //        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id   
  //     })
  //   }
  // },
  fiveBlocks: function (e) {
    var id = e.target.id
    var nav= e.target.dataset.nav
    console.log(e.target)
    if (id == 0) {
      this.setData({
        url: nav
      })
    }else {
      this.setData({
        url: nav+'?pro_id=' + id,
      })
    }
    wx.navigateTo({
      url: this.data.url,
    })
  },
  toBtoC: function (e) {
    var store_Id = e.currentTarget.id;
    wx.navigateTo({
      url: '../index/index?store_Id=' + store_Id,
    })
    app.d.store_Id = store_Id;
    // console.log(store_Id)
  },
  toGoodsDetail:function(e){
    var pro_id=e.target.id
wx.navigateTo({
  url: '../product/detail?pro_id=' + pro_id,
})
  },
  //点击加载更多
  getMore: function (e) {
    var that = this;
    if(this.data.currentTab==0){
      this.setData({
        types:'place',
        page1:this.data.page1
      })

    }else if(this.data.currentTab==1){
      this.setData({
        types: 'num',
        page2: this.data.page2
      })

    }else{
      this.setData({
        types: 'grade',
        page3: this.data.page3
      })
    }
    wx.request({
      url: app.d.ceshiUrl + '/Api/BIndex/show_getmore',
      method: 'post',
      data: {
        page: (that.data.currentTab == 0 ? that.data.page1 : (that.data.currentTab == 1 ? that.data.page2 : that.data.page3)),
        type:that.data.types,
        fid:0,
        long2: typeof(that.data.long2)=='undefined'?0:that.data.long2,
        la2: typeof(that.data.la2)=='undefined'?0:that.data.la2,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        var place_store = res.data.place_store;
        var num_store = res.data.num_store;
        var grade_store = res.data.grade_store;
        var num = res.data.num;
        if(num<=that.data.page1||num<=that.data.page2||num<=that.data.page3){
          that.setData({
              requestLoadingComplete: true,
              requestLoading: false
          })
        }
        if (place_store == '') {
          wx.showToast({
            title: '没有更多数据！',
            duration: 2000
          });
          that.setData({
              requestLoadingComplete: true,
              requestLoading: false
          })
          return false;
        } else if (that.data.currentTab==0){
          that.setData({
            page1: that.data.page1 + 1,
            sseller: that.data.sseller.concat(place_store),
          });
          console.log('changdu'+that.data.sseller.length)
         setTimeout(function () {
          that.setData({
            height: 97 * that.data.sseller.length,
          });
         }, 500);
          console.log(that.data.height)
          console.log('changdu'+that.data.sseller.length)
        } else if (that.data.currentTab == 1) {
          that.setData({
            page2: that.data.page2 + 1,
            store1: that.data.store1.concat(num_store),
          });
          that.setData({
            height1: 176 * that.data.store1.length,
          })
          for (var i = 0; i < num_store.length; i++) {
            var la1 = parseFloat(num_store[i].latitude)
            var long1 = parseFloat(num_store[i].longitude)
            var rad1 = la1 * Math.PI / 180.0;
            var rad2 = that.data.la2 * Math.PI / 180.0;
            var a = rad1 - rad2;
            var b = long1 * Math.PI / 180.0 - that.data.long2 * Math.PI / 180.0;
            var r = 6378.137;
            var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
            that.setData({
              distance2: that.data.distance2.concat({ distance: distance.toFixed(1) })
            })
          }
        } else if (that.data.currentTab == 2) {
          that.setData({
            page3: that.data.page3 + 1,
            store2: that.data.store2.concat(grade_store),
          });
          that.setData({
            height2: 176 * that.data.store2.length,
          })
          for (var i = 0; i < grade_store.length; i++) {
            var la1 = parseFloat(grade_store[i].latitude)
            var long1 = parseFloat(grade_store[i].longitude)
            var rad1 = la1 * Math.PI / 180.0;
            var rad2 = that.data.la2 * Math.PI / 180.0;
            var a = rad1 - rad2;
            var b = long1 * Math.PI / 180.0 - that.data.long2 * Math.PI / 180.0;
            var r = 6378.137;
            var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
            that.setData({
              distance3: that.data.distance3.concat({ distance: distance.toFixed(1) })
            })
          }
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络不给力！',
          duration: 2000
        });
      }
    })
  },
    //跳转扫一扫
    scancode: function () {
        var that = this;



        wx.scanCode({
            success: (res) => {
                var result = res.result;
                wx.request({
                    url: app.d.ceshiUrl + '/Api/Scan/goodone',
                    method: 'post',
                    data: {
                        pro_number:result,
                    },
                    header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    success: function (res) {
                        console.log(res);
                        if(res.data.good_id==0){
                            wx.navigateTo({
                                url: '../company_index/company_index',
                            })
                        }else{
                            var product_id = res.data.good_id;
                            wx.navigateTo({
                                url: '../product/detail?pro_id=' + product_id,
                            })
                        }


                    },
                    fail: function (e) {
                        wx.showToast({
                            title: '网络不给力！',
                            duration: 2000
                        });
                    },
                })
            }
        })
    },
  call: function () {
    wx.makePhoneCall({
      phoneNumber: '13146666946',
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
 // 按下按钮的时候触发
  startrecorderHandel() {
    // 开始录音
    recorderManager.start({
    });
  },
  // 松开按钮的时候触发-发送录音
  sendrecorderHandel() {
    // 结束录音
    recorderManager.stop();
  },
  closeActModal: function() {

        this.setData({
            act_modal_list:'a'
        })
  },
    /**
   * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
      
        let that = this;
        if (that.data.requestLoading && !that.data.requestLoadingComplete) {
            that.setData({
                requestPageNum: that.data.requestPageNum + 1,  //每次触发上拉事件，把requestPageNum+1
            })
            that.getMore();
        }
        console.log("page上拉触底")
        console.log(that.data.requestPageNum)
  },
  onShareAppMessage: function () {
      return {
        //title: '' + this.data.store.name,
        path: '/pages/FirstPage/FirstPage',
        //imageUrl: '' + this.data.imgUrls[0].photo,
        success: function (res) {
      
          // 分享成功
        },
        fail: function (res) {
          // 分享失败
        }
    }
  },
  getPhoneNumber(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },

})
